<?php
namespace app\company\controller;
use think\Controller;
use think\Db;
use think\Session;
class Index extends Controller
{
    //登录页面
    public function index()
    {
        return $this->fetch();
    }
    //处理登录
    public function login()
    {
        $data = [
            "company_user_phone"=>input('company_user_phone'),
            "company_user_pw"=>md5(input('company_user_pw')),
        ];
        // dump($data);
        // exit;
        $res = Db::name('company_user')->where($data)->find();
        // dump($res);
        // exit;
        if($res==null){
            return $this->error('账号密码错误');
        }else{
            $res1 = Db::name('company')->where('company_id',$res["company_id"])->find();
            Session::set("company_id",$res["company_id"]);
            Session::set("company_name",$res1["company_name"]);
            $this->success('登录成功','company/index');
        }
    }
    //注销功能
    public function logout(){
        Session::clear();
        $this->redirect("index/index");
    }
    public function reg()
    {
        return $this->fetch();
    }
    public function reg1()
    {
       
        $company_phone = input('company_user_phone');
        $a = Db::name('company')->where('company_phone',$company_phone)->find();
        if($a == null)
        {
            $data = ['company_phone' => input('company_user_phone'),'company_status' => 0,];
            Db::table('company')->insert($data);
            $companyId = Db::name('company')->getLastInsID();
            $data1 = 
            [
                'company_user_phone' => input('company_user_phone'),
                'company_user_pw' => md5(input('company_user_pw')),
                'company_id' =>$companyId,
            ];
            $res = Db::table('company_user')->insert($data1);
               if($res)
                {
                    return $this->success('注册成功','index/index');
                }
                else
                {
                    return $this->error('注册失败');
                }
        }
        else
        {
            return $this->error('账号已经被注册');
        }
       
    }
}
